package com.rockpaperscissorlhh.many;

/**
 * @description:
 * @author: lhh
 * @time: 2022/6/14
 */

//评判模块

class Multithreading extends Thread {
    static int timeAll=10;	//设置玩的次数。
    int player1win=0;	//甲方胜利的次数
    int player2win=0;	//乙方胜利的次数
    int noBodyWin=0;	//平局的次数
    Player play=new Player();
    Multithreading(Player play)         //带参数的构造方法
    {
        this.play=play;		//将改次比赛作为评判对象
    }
    public void run()			//Judge的run()方法
    {
        while(true)
        {
            synchronized(play)     //对本次比赛加锁
            {
                if(Player.turn1Res!=null&&Player.turn2Res!=null)   //判断两人是否两人都出过拳
                {
                    goJudge();					//评判
                    Player.turn1Res=null;		//清空 两人出拳记录
                    Player.turn2Res=null;
                    System.out.println("");
                    play.notifyAll();			//清空等待队列
                    timeAll--;					//总次数-1
                    try
                    {
                        sleep(1000);				//每次比赛的间隔时间
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            if(Multithreading.timeAll==0)				//比赛结束，打印比赛结果
            {
                System.out.println("总共进行10次比赛，"+"甲胜利"+player1win+"次,"+
                        "乙胜利"+player2win+"次,"+"平局"+noBodyWin+"次。");
                timeAll--;
            }
        }
    }
    //根据规则的评判方法
    private void goJudge()
    {
        if(Player.turn1Res=="石头"  &&  Player.turn2Res=="剪刀"||
                Player.turn1Res=="剪刀"  &&  Player.turn2Res=="布"  ||
                Player.turn1Res=="布"    &&  Player.turn2Res=="石头" )
        {
            System.out.println(Player.turn1Player+"获胜!");
            count(Player.turn1Player);
        }
        else if (Player.turn1Res  ==  Player.turn2Res)
        {
            System.out.println("平局!");
            count("平局");
        }
        else
        {
            System.out.println(Player.turn2Player+"获胜!");
            count(Player.turn2Player);
        }
    }
    //统计方法
    private void count(String name)
    {
        if(name.equals("甲方") ) player1win++;					//78
        else if(name.equals("乙方") ) player2win++;				//79
        else noBodyWin++;
    }
}
//比赛模块
class Player implements Runnable
{
    static boolean everPlay=false;		//判断是否有人出过拳
    static String turn1Res;				//保存第一个人的出拳结果
    static String turn2Res;				//保存第二个人的出拳结果
    static String turn1Player;			//保存第一个出拳人
    static String turn2Player;			//保存第二个出拳人
    void play()							//出拳方法
    {
        turn2Res=turnPlay(rd());
        System.out.println(":我出的是"+turn2Res);
    }
    private double rd()					//随机数方法，使出拳随机
    {
        return Math.random()*10;
    }
    private String turnPlay(double rd)	//从随机数，产生出拳
    {
        if(rd>=0&rd<=3.333)
        {
            return "剪刀";
        }
        else if(rd>3.333&rd<=6.666)
        {
            return "石头";
        }
        return "布";
    }
    private String GetTurnPlayer()			//获得当前出拳人
    {
        return Thread.currentThread().getName();
    }
    public void run()				//当次比赛的run()方法
    {
        while(Multithreading.timeAll>0)			//判断比赛是否结束
        {
            synchronized(this)			//对本次比赛加锁
            {
                System.out.print(GetTurnPlayer());		//打印当前准备出拳人
                if(everPlay)
                {
                    //已经有人出过拳了
                    turn2Player=GetTurnPlayer();		//保存第二个出拳人
                    turn1Res=turn2Res;					//让先出拳的结果保存到turn1Res
                    play();								//出拳
                    everPlay=false;						//出拳结束
                    try
                    {
                        wait();							//第二个人加入等待队列
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    //还没有人出拳
                    turn1Player=GetTurnPlayer();		//保存第一个出拳人
                    play();								//出拳
                    everPlay=true;						//设置有人出过拳了
                    try
                    {
                        wait();							//第一个人加入等待队列
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
class test
{
    public static void main(String[] args)						//入口函数
    {
        Player player=new Player();								//创建新比赛
        Multithreading judge=new Multithreading(player);							//创建本次比赛裁判
        judge.setDaemon(true);									//将裁判后台运行
        Thread playerOne=new Thread(player);					//创建甲方
        Thread playerTwo=new Thread(player);					//创建乙方
        playerOne.setName("甲方");								//赋名
        playerTwo.setName("乙方");
        playerOne.start();										//启动线程甲
        playerTwo.start();										//启动线程乙
        judge.start();											//裁判启动
    }
}




